/**
 @Name：选课管理
 @Author：tao
 */

layui.define(['table', 'form'], function (exports) {
  var $ = layui.jquery,
    layer = layui.layer,
    form = layui.form;
  var table = layui.table;

  function myUpdate(obj, tableData, callback) {
    //'编辑'按钮的回调
    var tr = $(obj.tr);
    var data = obj.data;
    var dialog = $('#Dialog');

    //弹窗, 包含'yes'和'success'两个回调函数
    layer.open({
      type: 1,
      title: '修改课程信息',
      content: dialog,
      area: ['550px', '350px'],
      btn: ['确定', '取消'],
      resize: false,
      yes: function (index, layero) {
        //弹窗'确定'按钮的回调
        //获取iframe元素的值
        let d = form.val("layuiadmin-form-replys");
        let [course_id, class_course_id] = $("#course option:selected").val().split('-')

        d.class_course_id = class_course_id;
        d.course_id = course_id
        form.render('select')
        console.log(d)
        layer.close(index);
        callback && callback(d)
      },
      //弹窗弹出后的回调
      success: function (layero, index) {
        //将行的数据传到弹窗
        $('select[name="name"]').empty();
        $('select[name="course"]').empty();
        renderSelect($('select[name="name"]'), $('select[name="course"]'), tableData, data.student_id, data.class_name_id, data.course_id)
        form.render()
      }
    });
  }


  /**
   * 获取多选框的id, 以及需删除的行对象
   * @param {object} delData  选择的所有行数据
   */
  function getDataId(delData) {
    var arr = [];
    for (var i in delData) {
      if (delData[i].checked || !delData[i].hasOwnProperty('checked')) {
        arr.push(delData[i].data.student_id)
      } else {
        //删checked为false的行
        delete delData[i]
      }
    }
    return {
      delData,
      arr
    }
  }


  function batchadd(tableData, callback) {
    var dialog = $('#Dialog');
    var is_must = dialog.find('input[name="is_must"]')[0];
    layer.open({
      type: 1,
      title: '添加课程',
      content: dialog,
      area: ['550px', '350px'],
      btn: ['确定', '取消'],
      resize: false,
      yes: function (index, layero) {
        let d = form.val("layuiadmin-form-replys");
        console.log(d)
        //向服务器发起请求  添加数据
        if (isNull(d)) {
          let [course_id, class_course_id] = d.course.split('-')
          d.class_course_id = class_course_id;
          d.course_id = course_id
          console.log(d)
          layer.close(index)
          callback && callback(d)
        } else {
          layer.msg('内容不能为空')
        }
      },
      success: function (layero, index) {
        //清空表单
        $('select[name="name"]').empty();
        $('select[name="course"]').empty();
        $('select[name="name"]').append("<option value=''>请选择</option>");
        $('select[name="course"]').append("<option value=''>请选择</option>");
        renderSelect($('select[name="name"]'), $('select[name="course"]'), tableData)
        form.render()
      }
    })
  }

  function renderSelect($dom, $dom2, tableData, value1, value2, value3) {
    let str = ''
    let str2 = ''
    let {
      students,
      courses
    } = tableData
    students.forEach(t => {
      str += `<option value="${t.user_id}" >${t.name}(${t.user_id})</option>`
    })
    courses.forEach(t => {
      str2 += `<option value="${t.course_id}-${t.class_course_id}" >${t.name}(${t.course_id}-${t.class_course_id})</option>`
    })

    $dom.append(str)
    $dom2.append(str2)
    if (value1 && value2) {
      console.log($dom.find(`option[value='${value1}']`))
      $dom.find(`option[value='${value1}']`).prop("selected", true);
      $dom2.find(`option[value='${value3}-${value2}']`).prop("selected", true);
      form.render("select")
    }
  }


  function batchdel(delData, callback) {
    var checkStatus = table.checkStatus('LAY-app-forumreply-list'),
      checkData = checkStatus.data; //得到选中的数据
    if (checkData.length === 0) {
      return layer.msg('请选择数据');
    } else {
      layer.confirm('确定删除吗？', function () {
        callback && callback(getDataId(delData))
      })
    };
  }

  function isNull(obj) {
    for (let item in obj) {
      if (obj[item] === '') {
        return false
      }
    }
    return true
  }

  function getMyData() {
    return new Promise((resolve, reject) => {
      $.ajax({
        url: 'http://localhost:8589/api/Selected/getMyData',
        success: resolve,
        fail: reject
      })
    })
  }


  exports('selected', {
    getDataId,
    myUpdate,
    batchadd,
    batchdel,
    getMyData
  })
});